package com.lanou.student.service.impl;

import com.lanou.student.bean.Student;
import com.lanou.student.mapper.StudentMapper;
import com.lanou.student.mapper.impl.StudentMapperImpl;
import com.lanou.student.service.StudentService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zyj
 * @date 2022年06月01日 9:17
 */
public class StudentServiceImpl implements StudentService {
    private StudentMapper studentMapper;


    public StudentServiceImpl() {
        studentMapper = new StudentMapperImpl();
    }


    @Override
    public List<Student> getStudents() {
        return studentMapper.getStudents();
    }

    @Override
    public boolean add(Student s) {
        return studentMapper.add(s);
    }

    @Override
    public boolean delete(int id) {
        return studentMapper.delete(id);
    }

    @Override
    public Student getStudent(int id) {
        List<Student> students = getStudents()
                .stream().filter(student -> student.getId() == id)
                .collect(Collectors.toList());
        return students.isEmpty() ? null : students.get(0);
    }

    @Override
    public boolean update(Student stu) {
        return studentMapper.update(stu);
    }

    @Override
    public int getId() {
        List<Student> students = studentMapper.getStudents();
        if (students.isEmpty()){
            return 1;
        }
        return students.get(students.size()-1).getId() + 1;
    }
}
